package com.zhen.lucene.meetlucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.*;

public class Indexer {

    private IndexWriter writer;


    public Indexer(String indexDir) throws IOException {
        Directory dir = FSDirectory.open(new File(indexDir));
        writer = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
    }

    public static void main(String[] args) throws IOException {
//        索引输出目录
        String indexDir = "lucene-in-action-1/src/main/resources/index";
//        要生成索引的文件目录
        String dataDir = "lucene-in-action-1/src/main/resources/data";

//        开始时间
        long start = System.currentTimeMillis();


        Indexer indexer = new Indexer(indexDir);
//        生成的索引数
        int numIndexed;
        try{
            numIndexed = indexer.index(dataDir,new TextFieldFilter());
        }finally {
            indexer.close();
        }

//        结束时间
        long end = System.currentTimeMillis();

//        打印生成了多少个索引花了多长时间
        System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
    }

    /**
     * 生成索引
     * @param dataDir 文件目录
     * @param filter txt文件过滤器
     * @return
     * @throws IOException
     */
    private int index(String dataDir,FileFilter filter) throws IOException {
        File[] files = new File(dataDir).listFiles();
        for(File f : files){
            if(!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() && (filter == null ||filter.accept(f))){
                indexFile(f);
            }
        }
        return writer.numDocs();
    }

    /**
     * 关闭索引生成器
     * @throws IOException
     */
    public void close() throws IOException {
        writer.close();
    }

    /**
     * 根据文件生成文档
     * @param f 要生成文档的文件
     * @return 根据文件生成的文档
     * @throws IOException
     */
    protected Document getDocument(File f) throws IOException {
        Document doc = new Document();
        doc.add(new Field("contents",new FileReader(f)));
        doc.add(new Field("filename",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
        doc.add(new Field("fullpath",f.getCanonicalPath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
        return doc;
    }

    /**
     * 根据文件生成索引
     * @param f 要生成所以的文件
     * @throws IOException
     */
    private void indexFile(File f) throws IOException {
        System.out.println("Indexing " + f.getCanonicalPath());
        Document doc = this.getDocument(f);
        writer.addDocument(doc);
    }

    /**
     * txt文件过滤器
     */
    private static class TextFieldFilter implements FileFilter{

        public boolean accept(File pathname) {
            return pathname.getName().toLowerCase().endsWith(".txt");
        }
    }
}
